package com.sap.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.sap.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    private String timeout = "30m";
    //在支付宝创建的应用的id
    private   String app_id = "2021000117619897";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCG99/s7z8AmNnQ14oJBoZNfqY0PM573iJ0W52qZ+C/pSDfjvxjuYWob5gxt+YgxrH59MIPyLe50ntrbH7vbuIPdfACp6VNsH44mzDOF2u1+CDzoIMGED7Pux30s0Y57+SDu8n2270mZZJkazA3/DqjoMvrNaQfWz3//yiqMF83hWwHe6g5tqAXDRIKIH7EH/2DjQj9CsDIp5aksNvp3nSWp++kuPtKh10tMSaB6IR7WJjw9vdbTtjpSYRqH2xfH/rzo26LmRz2bmebyPsGKEUlM6op+g8vdpqps0a1Hm77g0LaxOIw7oLagX26+O9BvmblxVyfjZU0PYsZilROcetjAgMBAAECggEAPGDLFe15W8UFO/4QTDVVA3ny1E+Xd6ZJ94+8mqzZyCIQ8LHAisRF3LH/fc5s7qC0ocMyxWjfeAuHFul0Z5xNGOmDs0xYEMcx0BTs4mVr0/3qj9JwitfdoYRtxDgm5GhC7AOoN/zZQ0IRGUcyTPlTKjwWvWRDxZtFwy3cfTYigm1MZ/VycNJ5rWPTKObz/01B7+71/SKYSrq4Gu/tlAvBF/O+T1RuNa7muU/fB3pAuP+LYL2+r14QTF0eP1oJ7Vf8xnWUUZ/vE9xfmpOkxL3u0+0Y6sG3D32qRExYhobLlePVPpJUgGQ+Py7Yndvn7NtIp9OnmXBzueEvO29W3hMcQQKBgQDsO6FdmuEgs43ddQiMGLZ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MA2bmZXx4I4nvPiMZ2NBi52mrr1leJj5W0C4SIxe+eM2k7Ag0vNTWO14vdd1mZI96PCGU+YVdey8R9AT9zPtvVRJymC+SGDC+riy1mGcfPi8BAnFxyNCBEbAMWnopbdDGtnzWokUZywLYlUBVYWrkmSMmWKEJ+B5onTPTvRzFhwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://cab2hp.natappfree.cc";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.mall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
